//
//  PopoverAnimaor.swift
//  weibo
//
//  Created by 叶子韬韬 on 2017/4/22.
//  Copyright © 2017年 叶子韬韬. All rights reserved.
//

import UIKit

class PopoverAnimaor: NSObject {

    internal var isPresent:Bool = false
    // MARK:- #注释> 对外提供属性
    var presentedFram:CGRect = CGRect.zero
    
    // MARK:- #注释>对外提供属性
    var callBack : ((_ presented:Bool) ->())?
    
    // MARK:- #注释>自定义构造函数
    init(callBack:@escaping (_ presented:Bool) ->()) {
        self.callBack = callBack
    }
}
// MARK:- #注释> 自定义转场的代理方法
extension PopoverAnimaor:UIViewControllerTransitioningDelegate {
    // MARK:- #注释> 设置frame
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        let presenttation = MyPresentationController(presentedViewController: presented, presenting: presenting)
        presenttation.presentedFram = presentedFram
        
        return presenttation
    }
    
    // MARK:- #注释>自定义弹出动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        // 对闭包进行赋值
        callBack!(isPresent)
        return self
    }
    // MARK:- #注释>自己定义消失动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        // 对闭包进行赋值
        callBack!(isPresent)
        return self
    }
}

extension PopoverAnimaor:UIViewControllerAnimatedTransitioning{
    // MARK:- #注释>动画执行的时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    // MARK:- #注释>获取转场的上下文 可以通过转场上下文获取弹出的view和消失的view
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        isPresent ? animationForPresentedView(using: transitionContext) :animationForDismissedView(using: transitionContext)
    }
    
    // MARK:- #注释> 自定义弹出动画
    internal func animationForPresentedView(using transitionContext: UIViewControllerContextTransitioning){
        //获取弹出view
        let presentView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
        //将弹出view添加到containerView中
        transitionContext.containerView.addSubview(presentView)
        
        //执行动画
        presentView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.0)
        presentView.layer.anchorPoint = CGPoint.init(x: 0.5, y: 0)
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            presentView.transform = CGAffineTransform.identity
        }) { (isFinished:Bool) in
            //必须告诉转场上下文 你已经完成动画
            transitionContext.completeTransition(true)
        }
        
    }
    
    // MARK:- #注释> 自定义消失动画
    internal func animationForDismissedView(using transitionContext: UIViewControllerContextTransitioning){
        //获取消失view
        let dismissView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
        
        //执行动画
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            dismissView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.0001)
        }) { (isFinished:Bool) in
            dismissView.removeFromSuperview()  //从父控件直接移除
            //必须告诉转场上下文 你已经完成动画
            transitionContext.completeTransition(true)
        }
        
    }
    
    
}
